package com.enjoy.xbase.xui.load;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import androidx.annotation.LayoutRes;

import com.enjoy.xbase.R;
import com.enjoy.xbase.xui.listener.ILoadRefresh;

/**
 * 描述：
 *    load视图
 * 介绍：
 * 创建方式：可通过xml布局方式创建、可依赖已有view通过LoadView.build()构建
 * 创建完成后使用前需指定BaseLoad 否则为默认
 */
public final class LoadView extends RelativeLayout {
    //数据加载界面
    protected View loadView;
    //遮罩层数据加载视图
    protected View loadingView;
    //无数据视图
    protected View emptyView;
    //失败视图
    protected View failView;
    //背景颜色
    private Integer backgroundColor;

    private BaseLoad load;

    public ILoadRefresh loadRefresh;

    /**
     * 描述：为某一视图创建load视图
     * @param view
     * @return
     */
    public static LoadView build(View view) {
        FrameLayout wrapper = new FrameLayout(view.getContext());
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp != null) {
            wrapper.setLayoutParams(lp);
        }
        if (view.getParent() != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            int index = parent.indexOfChild(view);
            parent.removeView(view);
            parent.addView(wrapper, index);
        }
        LayoutParams newLp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        wrapper.addView(view, newLp);

        LayoutParams newLoadLp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        LoadView loadView=new LoadView(view.getContext());
        loadView.setLayoutParams(newLoadLp);
        wrapper.addView(loadView);
        return loadView;
    }

    public LoadView(Context context) {
        super(context);
    }

    public LoadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public LoadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }
    /**
     * 描述：初始化默认背景
     */
    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LoadView);
        if (a!=null&&a.hasValue(R.styleable.LoadView_bgColor)){//判断是否设置
            backgroundColor = a.getColor(R.styleable.LoadView_bgColor, Color.parseColor("#ffffff"));
        }
    }
    private void initDefaultLoad(){
        if (this.load==null) {
            this.load = new DefaultLoad();
            this.load.setLoadView(this);
        }
    }
    public synchronized void setLoad(BaseLoad load) {
        if (this.loadView!=null){
            removeView(this.loadView);
            this.loadView=null;
        }
        if (this.loadingView!=null){
            removeView(this.loadingView);
            this.loadingView=null;
        }
        if (this.emptyView!=null){
            removeView(this.emptyView);
            this.emptyView=null;
        }
        if (this.failView!=null){
            removeView(this.failView);
            this.failView=null;
        }
        this.load = load;
        this.load.setLoadView(this);
    }
    public void showLoad(){
        showLoad(null);
    }
    public void showLoading(){
        showLoading(null);
    }
    public void showFail(){
        showFail(null);
    }
    public void showEmpty(){
        showEmpty(null);
    }
    public void showData() {
        hideAll(null);
    }
    public synchronized void showLoad(Object param) {
        initDefaultLoad();
        hideAll(loadView);
        if (loadView == null) {
            loadView = createView(load.getLoadLayoutId());
            if (loadView!=null) {
                addView(loadView);
                setViewBackgroundColor(loadView);
                loadView.setVisibility(View.VISIBLE);
                load.onLoadShow(loadView,true,param);
            }
        }else{
            if (loadView.getVisibility()==GONE){
                load.onLoadShow(loadView,false,param);
            }
            loadView.setVisibility(View.VISIBLE);
        }
    }
    public synchronized void showLoading(Object param) {
        initDefaultLoad();
        hideAll(loadingView);
        if (loadingView == null) {
            loadingView = createView(load.getLoadingLayoutId());
            if (loadingView!=null) {
                addView(loadingView);
                loadingView.setVisibility(VISIBLE);
                load.onLoadingShow(loadingView,true,param);
            }
        }else{
            if (loadingView.getVisibility()==GONE){
                load.onLoadingShow(loadingView,false,param);
            }
            loadingView.setVisibility(View.VISIBLE);
        }
    }
    public synchronized void showFail(Object param) {
        initDefaultLoad();
        hideAll(failView);
        if (failView == null) {
            failView = createView(load.getFailLayoutId());
            if (failView!=null) {
                addView(failView);
                setViewBackgroundColor(failView);
                failView.setVisibility(VISIBLE);
                load.onFailShow(failView,true,param);
            }
        }else  {
            if (failView.getVisibility()==GONE){
                load.onFailShow(failView,false,param);
            }
            failView.setVisibility(View.VISIBLE);
        }
    }
    public synchronized void showEmpty(Object param) {
        initDefaultLoad();
        hideAll(emptyView);
        if (emptyView == null) {
            emptyView = createView(load.getEmptyLayoutId());
            if (emptyView!=null) {
                addView(emptyView);
                setViewBackgroundColor(emptyView);
                emptyView.setVisibility(VISIBLE);
                load.onEmptyShow(emptyView,true,param);
            }
        }
        if (emptyView != null) {
            if (emptyView.getVisibility()==GONE){
                load.onEmptyShow(emptyView,false,param);
            }
            emptyView.setVisibility(View.VISIBLE);
        }
    }
    private View createView(@LayoutRes int layoutId){
        return LayoutInflater.from(getContext()).inflate(layoutId, this, false);
    }
    private void hideAll(View showView) {
        if (loadView != null&&loadView!=showView&&loadView.getVisibility()==VISIBLE) {
            load.onLoadHide(loadView);
            loadView.setVisibility(View.GONE);
        }
        if (loadingView != null&&loadingView!=showView&&loadingView.getVisibility()==VISIBLE) {
            load.onLoadingHide(loadingView);
            loadingView.setVisibility(View.GONE);
        }
        if (failView != null&&failView!=showView&&failView.getVisibility()==VISIBLE) {
            load.onFailHide(failView);
            failView.setVisibility(View.GONE);
        }
        if (emptyView != null&&emptyView!=showView&&emptyView.getVisibility()==VISIBLE) {
            load.onEmptyHide(emptyView);
            emptyView.setVisibility(View.GONE);
        }
    }
    public void setBackgroundColor(int color) {
        this.backgroundColor = color;
        setViewBackgroundColor(loadView);
        setViewBackgroundColor(failView);
        setViewBackgroundColor(emptyView);
    }
    public void setLoadRefresh(ILoadRefresh loadRefresh) {
        this.loadRefresh=loadRefresh;
    }
    private void setViewBackgroundColor(View view){
        if (view != null&&backgroundColor!=null) {
            view.setBackgroundColor(backgroundColor);
        }
    }

}
